#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;
struct TreeNode {
     int val;
   TreeNode *left;
     TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
private:
    struct unit {

    };
    void findFrequentTreeSumCore(TreeNode* root, unordered_map<int,int>&hashmap,int& max_count) {
        if (root == NULL)
            return;
        findFrequentTreeSumCore(root->left, hashmap, max_count);
        findFrequentTreeSumCore(root->right, hashmap, max_count);
        int sum = root->val;
        if (root->left != NULL) sum += root->left->val;
        if (root->right != NULL) sum += root->right->val;
        ++hashmap[sum];
        root->val = sum;
        max_count = max(max_count, hashmap[sum]);
    }
public:
    vector<int> findFrequentTreeSum(TreeNode* root) {
        unordered_map<int, int>hashmap;
        int max_ = 0;
        findFrequentTreeSumCore(root, hashmap, max_);

        vector<int>res;
        for (auto m : hashmap) {
            if (m.second == max_)
                res.push_back(m.first);
        }
        return res;
    }
};